package cn.hn.comm.ibatis;

import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class BaseDAO extends SqlMapClientDaoSupport implements IDAO {

	/**
	 * 删除
	 */
	public int delete(String nameSpace, String sqlName, Object param) {
		if (param != null) {
			return getSqlMapClientTemplate().delete(
					getStatementName(nameSpace, sqlName), param);
		} else {
			return getSqlMapClientTemplate().delete(
					getStatementName(nameSpace, sqlName));
		}
	}

	/**
	 * 插入
	 */
	public Object insert(String nameSpace, String sqlName, Object param) {
		if (param != null) {
			return getSqlMapClientTemplate().insert(
					getStatementName(nameSpace, sqlName), param);
		} else {
			return getSqlMapClientTemplate().insert(
					getStatementName(nameSpace, sqlName));
		}
	}

	
	/**
	 * 查询
	 */
	public Object query(String nameSpace, String sqlName, Object param) {
		if (param != null) {
			return getSqlMapClientTemplate().queryForObject(
					getStatementName(nameSpace, sqlName), param);
		} else {
			return getSqlMapClientTemplate().queryForObject(
					getStatementName(nameSpace, sqlName));
		}
	}

	/**
	 * 查询列表
	 */
	public List queryList(String nameSpace, String sqlName, Object params) {
		if (params != null) {
			return getSqlMapClientTemplate().queryForList(
					getStatementName(nameSpace, sqlName), params);
		} else {
			return getSqlMapClientTemplate().queryForList(
					getStatementName(nameSpace, sqlName));
		}
	}

	/**
	 * 查询列表分页
	 */
	public List queryList(String nameSpace, String sqlName, Object params,
			int skipResults, int maxResults) {
		if (params != null) {
			return getSqlMapClientTemplate().queryForList(
					getStatementName(nameSpace, sqlName), params, skipResults,
					maxResults);
		} else {
			return getSqlMapClientTemplate().queryForList(
					getStatementName(nameSpace, sqlName), skipResults,
					maxResults);
		}
	}

	
	/**
	 * 更新
	 */
	public int update(String nameSpace, String sqlName, Object params) {
		if (params != null) {
			return getSqlMapClientTemplate().update(
					getStatementName(nameSpace, sqlName), params);
		} else {
			return getSqlMapClientTemplate().update(
					getStatementName(nameSpace, sqlName));
		}
	}

	
	/**
	 * 拼域
	 * @param nameSpace
	 * @param sqlName
	 * @return
	 */
	public String getStatementName(String nameSpace, String sqlName) {
		String statementName = null;
		if (nameSpace != null) {
			statementName = nameSpace + "." + sqlName;
		} else {
			statementName = sqlName;
		}

		return statementName;
	}

}
